"""
Case Type   : CM
Case Name   : 使用gs_guc set -Z cmagent -c方式修改参数enable_log_compress值为yes\on\true\1
Create At   : 2024.6
Owner       : haomeng802
Description :
    1、查看集群状态 cm_ctl query -Cidvp
    2、查询参数enable_log_compress默认值
    3、修改参数值为yes
    4、修改参数值为on
    5、修改参数值为true
    6、修改参数值为1
    7.恢复集群
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、默认值查询成功为on
    3、设置成功
    4、设置成功
    5、设置成功
    6、设置成功
    7、恢复集群成功
History     :
    2024.4.11 ningyali 增加前置（安装cm）和后置（卸载cm）操作
"""

import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CmServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.param = 'enable_log_compress'
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_cm_server(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '----step2:查询参数enable_log_compress默认值; expect:默认值查询成功为on----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'cm_ctl list --param --agent | grep {self.param}'
        self.log.info(cmd)
        show_param1 = self.user_nodes[0].sh(cmd).result()
        self.log.info(show_param1)
        self.default_value = show_param1.splitlines()[0].split('=')[-1].strip()
        self.assertEqual('on', self.default_value, '参数默认值不为2')

        text = '-----step3:修改参数值为yes expect:设置成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -c "{self.param} = yes";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置失败')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数设置成功')

        text = '-----step4:修改参数值为on expect:设置成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -c "{self.param} = on";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置成功')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数设置成功')

        text = '-----step5:修改参数值为true expect:设置成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -c "{self.param} = true";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置成功')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数设置成功')

        text = '-----step6:修改参数值为1  expect:设置成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -c "{self.param} = 1";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置成功')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数设置成功')

        text = '-----step7:回复参数默认值，恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)

        reset = f'source {macro.DB_ENV_PATH};' \
                f'cm_ctl set --param --agent -k "{self.param}={self.default_value}";' \
                f'cm_ctl reload --param --agent'
        self.log.info(reset)
        set_res = self.user_nodes[0].sh(reset).result()
        self.log.info(set_res)
        self.assertIn(self.constant.cma_set_success_msg, set_res, '参数设置成功')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数设置成功')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
